package com.ims.controller;

import com.alibaba.fastjson.JSONObject;
import com.ims.pojo.Status;
import com.ims.service.StatusService;
import com.ims.util.PageResult;
import com.ims.util.QueryPageBean;
import com.ims.util.Result;
import com.ims.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zk
 * @date: 2022/9/4 16:00
 */
@RestController
@RequestMapping("/status")
public class StatusController {
    @Autowired
    private StatusService statusService;

    @GetMapping("/findAll")
    public Result<List<JSONObject>> findAll() {
        List<Status> statusList = statusService.list();

        List<JSONObject> result = new ArrayList<>();
        for(Status status : statusList){
            JSONObject item = new JSONObject();
            item.put("id", status.getId());
            item.put("device_id", status.getDeviceId());
            item.put("device_name", status.getDeviceName());
            item.put("status_info", JSONObject.parse(status.getStatusInfo()));
            item.put("event_time", status.getEventTime());
            result.add(item);
        }

        return new Result(true, StatusCode.OK, "查询成功", result);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result<PageResult> findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<JSONObject> pageResult = statusService.findPage(queryPageBean);
        return new Result<>(true, StatusCode.OK, "分页查询成功", pageResult);
    }
}
